﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleAppDemo {

    #region ReaderWriterLockSlim和Lock锁性能比较
    public class RwLock {
        private static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
        public static void ReadSomething()
        {
            rwl.EnterReadLock();
            try
            {
                Thread.Sleep(10);
            }
            finally
            {
                rwl.ExitReadLock();
            }
        }
        public static void WriteSomething()
        {
            rwl.EnterWriteLock();
            try
            {
                Thread.Sleep(100);
            }
            finally
            {
                rwl.ExitWriteLock();
            }
        }
    }

    public class RwNormalLock {
        private static object _lock = new object();
        public static void ReadSomething_lock()
        {
            lock (_lock)
            {
                //模拟读取信息
                Thread.Sleep(10);
            }
        }
        public static void WriteSomething_lock()
        {
            lock (_lock)
            {
                //模拟写入信息
                Thread.Sleep(100);
            }
        }
    }
    #endregion

    #region 多个线程同时写入同一个文件导致错误问题

    public class FileManage {
        public static int WritedCount;
        public static int FailedCount;
        private const string logFilePath = "log.txt";
        public static void WriteFileLog()
        {
            try
            {
                var now = DateTime.Now;
                var logContent = string.Format("Tid:{0}{1} {2}.{3}\r\n",
                    Thread.CurrentThread.ManagedThreadId.ToString().PadRight(4),
                    now.ToLongDateString(),
                    now.ToLongTimeString(),
                    now.Millisecond.ToString());
                File.AppendAllText(logFilePath, logContent);
                WritedCount++;
            }
            catch (Exception ex)
            {
                FailedCount++;
                Console.WriteLine(ex.Message);
            }
        }
    }

    public class FileRwLockManage {
        public static int WritedCount;
        public static int FailedCount;
        private static ReaderWriterLockSlim logWriterLock = new ReaderWriterLockSlim();
        public static void RwriteLog()
        {
            try
            {
                logWriterLock.EnterWriteLock();
                var logFilePath = "log.txt";
                var now = DateTime.Now;
                var logContent = string.Format("Tid:{0}{1} {2}.{3}\r\n",
                    Thread.CurrentThread.ManagedThreadId.ToString().PadRight(4),
                    now.ToLongDateString(),
                    now.ToLongTimeString(),
                    now.Millisecond.ToString());
                File.AppendAllText(logFilePath, logContent);
                WritedCount++;
            }
            catch
            {
                FailedCount++;
            }
            finally
            {
                logWriterLock.ExitWriteLock();
            }
        }
    }
    #endregion

}
